<template>
  <el-popover
    :placement="placement"
    trigger="click"
    width="300"
    popper-class="treeDiv"
    @hide="hide"
  >
    <el-input
      placeholder="输入关键字进行过滤"
      v-model="filterText"
      style="margin-bottom: 5px"
    ></el-input>
    <el-tree
      ref="popupTree"
      :show-checkbox="showCheckbox"
      @check="changeHandle"
      :data="data"
      :props="props"
      :node-key="nodeKey"
      :default-checked-keys="defaultCheckedKey"
      :filter-node-method="filterNode"
      :highlight-current="true"
      :expand-on-click-node="true"
      @current-change="currentChangeHandle"
    />
    <template #reference>
      <el-input v-model="prop" readonly :placeholder="placeholder" style="cursor: pointer" />
    </template>
  </el-popover>
</template>

<script lang="ts">
import { reactive, toRefs, watch, ref } from 'vue'
export default {
  name: 'PopupTreeInput',
  props: {
    data: {
      type: Array,
      required: true,
    },
    props: {
      type: Object,
      required: true,
    },
    prop: {
      type: String,
      default: '',
    },
    nodeKey: {
      type: String || Number,
      required: true,
    },
    placeholder: {
      type: String,
      default: '点击选择内容',
    },
    placement: {
      type: String,
      default: 'bottom-start',
    },
    defaultExpandAll: {
      type: Boolean,
      default: false,
    },
    defaultCheckedKey: {
      type: Array,
      default() {
        return []
      },
    },
    currentChangeHandle: {
      type: Function,
      default: null,
    },
    defaultKey: {
      type: String,
      default: '',
    },
    showCheckbox: {
      type: Boolean,
      default: false,
    },
    changeHandle: {
      type: Function,
      default: null,
    },
  },
  setup(props) {
    console.log(props.data)
    const data = reactive({
      // filterText:'' as any,
    })

    // watch(data.filterText, (val) => {
    //   console.log('val',val);

    // });
    let filterText = ref('')
    const popupTree = ref()
    watch(filterText, (newValue, oldValue) => {
      console.log('val', popupTree)
      popupTree.value.filter(newValue)
    })

    const filterNode = (value, data) => {
      if (!value) return true
      return data.name.indexOf(value) !== -1
    }
    const hide = () => {
      filterText.value = ''
    }
    return {
      ...toRefs(data),
      filterText,
      filterNode,
      popupTree,
      hide,
    }
  },
}
</script>

<style lang="scss">
.treeDiv {
  max-height: 400px;
  overflow-y: auto;
}
</style>
